package jafou.client.ui;

import jafou.client.remote.JafouService;
import jafou.client.remote.JafouServiceAsync;
import jafou.client.ui.objects.ContentPanel;
import jafou.client.ui.objects.ForumMenu;
import jafou.client.ui.objects.ForumsTree;
import jafou.client.ui.objects.MiscPanel;
import jafou.client.ui.objects.NewForumPanel;
import jafou.client.ui.objects.NewPostPanel;
import jafou.client.ui.objects.NewTopicPanel;

import java.util.HashMap;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ForumWidget extends SimplePanel{
	private int currentView;

	HashMap<String, String> userAttributes;

	private Panel bottomPanel;
	private Panel menuPanel = null;
	private MiscPanel miscPanel = null;
	private HorizontalSplitPanel mainPanel = null;
	private Panel treePanel = null;
	private ContentPanel contentPanel = null;
	private Panel footerPanel = null;
	private NewForumPanel newForumPopup = null;
	private NewTopicPanel newTopicPopup = null;
	private NewPostPanel newPostPopup = null;
	private ForumsTree forumTree = null;
	
	private final JafouServiceAsync handler = GWT.create(JafouService.class);
	private AsyncCallback<HashMap<String,String>> attrCallback = new AsyncCallback<HashMap<String,String>>() {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("could not connect to server.");
		}

		@Override
		public void onSuccess(HashMap<String, String> result) {
			userAttributes = result;
			continueSetup();
		}
	};

	public ForumWidget() {
		currentView = 0;
		userAttributes = new HashMap<String, String>();
		setSize("100%", "100%");
		setup();
	}

	public String getUserAttribute(String attrName) {
		return userAttributes.get(attrName);
	}

	public void setUserAttribute(String attrName, String value) {
		userAttributes.put(attrName, value);
	}

	private void setupPanels() {
		bottomPanel = new FlowPanel();
		bottomPanel.addStyleName("bottom-panel");

		menuPanel = new SimplePanel();
		menuPanel.addStyleName("menu-panel");

		miscPanel = new MiscPanel(this);

		mainPanel = new HorizontalSplitPanel();
		mainPanel.addStyleName("main-panel");
		mainPanel.setSplitPosition("15%");
		
		treePanel = new SimplePanel();
		treePanel.addStyleName("tree-panel");
		contentPanel = new ContentPanel(this);
		contentPanel.addStyleName("content-panel");

		mainPanel.add(treePanel);
		mainPanel.add(contentPanel);

		bottomPanel.add(menuPanel);
		bottomPanel.add(miscPanel);
		bottomPanel.add(mainPanel);
		add(bottomPanel);
	}

	public void setup() {
		handler.getUserAttributes(attrCallback);
	}
	
	private void continueSetup() {
		setupPanels();
		forumTree = new ForumsTree(this);
		menuPanel.add(new ForumMenu(this, true));
		treePanel.add(forumTree);
	}

	public void renderForum(String forumId) {
		contentPanel.showForum(forumId);
		miscPanel.showForumControls(forumId);
	}
	
	public void renderTopic(String topicId) {
		contentPanel.showTopic(topicId);
		miscPanel.showTopicControls(topicId);
	}

	public void renderNewForumPanel() {
		newForumPopup = new NewForumPanel(this);
		newForumPopup.show();
	}
	
	public void renderNewTopicPanel(String forumId) {
		newTopicPopup = new NewTopicPanel(this, forumId);
		newTopicPopup.show();
	}
	
	public void renderNewPostPanel(String topicId) {
		newPostPopup = new NewPostPanel(this, topicId);
		newPostPopup.show();
	}

	public void handleForumCreation(boolean result, String newForumId) {
		if (newForumPopup != null) {
			newForumPopup.hide();
			newForumPopup = null;
		}
		if(result){
			renderForum(newForumId);
			forumTree.update();
		}
	}
	
	public void handleTopicCreation(boolean result, String topicId){
		if (newTopicPopup != null) {
			newTopicPopup.hide();
			newTopicPopup = null;
		}
		if (result) {
			renderTopic(topicId);
		}
	}
	
	public void handlePostCreation(boolean result, String postId, String topicId) {
		if(newPostPopup != null) {
			newPostPopup.hide();
			newPostPopup = null;
		}
		if(result) {
			renderTopic(topicId);
		}
	}

	public void handleLoginLogout(String url) {
		Window.Location.assign(url);
	}

	public void handleForumDeletion() {
		forumTree.update();
		contentPanel.clearContents();
		miscPanel.showForumControls(null);
	}

	public void handleTopicDeletion(String result) {
		if(result != null && !"".equals(result.trim())) {
			renderForum(result);
		}
	}

}
